package day04;

import java.util.ArrayList;
import java.util.Iterator;

public class ListExercise {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Book("红楼梦",60.9,"红楼梦"));
        arrayList.add(new Book("三国演义",50.9,"三国演义"));
        arrayList.add(new Book("水浒传",39.9,"水浒传"));
        arrayList.add(null);
        arrayList.add(null);
        arrayList.add(null);
        System.out.println(arrayList.size());
        arrayList.remove(null);
        System.out.println(arrayList.size());
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            iterator.remove();
        }
        System.out.println(arrayList.size());
//        for (Object o :arrayList) {
//            System.out.println(o);
//        }
//        sort(arrayList);
//        for (Object o :arrayList) {
//            System.out.println(o);
//        }

    }
    public static void sort(ArrayList list) {
        //Book temp = null;
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = 0; j < list.size() - 1 - i; j++) {
                Book book1 = (Book) list.get(j);
                Book book2 = (Book) list.get(j + 1);
                if(book1.getPrice() > book2.getPrice()) {
                    list.set(j,book2);
                    list.set(j + 1,book1);
                }
            }
        }
    }
}
class Book {
    private String name;
    private double price;
    private String author;

    public Book(String name, double price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return
                "name=" + name + "\t" +
                "price=" + price + "\t" +
                "author=" + author;
    }


}